//
//  OSCTweetComposeViewModel.m
//  OSChina
//
//  Created by 8b410-2 on 17/1/9.
//  Copyright © 2017年 abb. All rights reserved.
//

#import <MBProgressHUD/MBProgressHUD.h>
#import "OSCTweetPubRequestParams.h"
#import "OSCTweetTools.h"
#import "OSCTweetComposeViewModel.h"
#import "GACompressionPicHandle.h"

@interface OSCTweetComposeViewModel ()

@property (nonatomic, copy, readwrite) NSString *content;

@property (nonatomic, copy, readwrite) NSArray *images;

@property (nonatomic, copy, readwrite) NSArray *imageDatas;

@property (nonatomic, assign, readwrite) NSInteger maxImagesCount;

@property (nonatomic, strong, readwrite) RACSignal *enableSendSignal;

@property (nonatomic, strong, readwrite) RACCommand *requestSendCommand;

@property (nonatomic, strong, readwrite) OSCTweetPubRequestParams *pubParams;

@end

@implementation OSCTweetComposeViewModel

- (instancetype)initWithServices:(id<OSCViewModelServices>)services params:(NSDictionary *)params {
    self = [super initWithServices:services params:params];
    if (self) {
        self.images = [NSArray new];
        self.imageDatas = [NSArray new];
        self.pubParams = [[OSCTweetPubRequestParams alloc] init];
    }
    return self;
}

- (void)initialize {
    [super initialize];
    
    self.enableSendSignal = [RACObserve(self, content) map:^id(NSString *content) {
        return @(content.length > 0 && content.length < maxStrLength);
    }];
}

- (NSInteger)maxImagesCount {
    return 9 - self.images.count;
}

- (void)dismiss {
    [self.services dismissViewModelAnimated:YES completion:nil];
}

- (void)handlePickerImagesFromAlbum:(NSArray *)photos {
    // 图片数据格式数组
    NSMutableArray* imagesData = [NSMutableArray arrayWithCapacity:photos.count];
    // 图片数组
    NSMutableArray* images = [NSMutableArray arrayWithCapacity:photos.count];
    for (UIImage* image in photos) {
        // 处理好的图片数据
        NSData* compressionImageData = nil;
        // 图片数据
        NSData* sourceImageData = UIImagePNGRepresentation(image);
        if (sourceImageData.length > stand_size) {//判断是否大于传输大小
            // 大于就按比例缩放
            CGFloat sourceImage_W = CGImageGetWidth(image.CGImage);
            CGFloat sourceImage_H = CGImageGetHeight(image.CGImage);
            CGFloat tagerImage_W = 0;
            CGFloat tagerImage_H = 0;
            if (sourceImage_W > CompressionMax_W) {
                tagerImage_W = CompressionMax_W;
                CGFloat scale = sourceImage_W / sourceImage_H;
                tagerImage_H = tagerImage_W / scale;
            }else{
                tagerImage_W = sourceImage_W;
                tagerImage_H = sourceImage_H;
            }
            GACompressionPicHandle* compressPicHandle = [GACompressionPicHandle shareCompressionPicHandle];
            compressionImageData = [compressPicHandle imageByScalingAndCroppingForSize:(CGSizeMake(tagerImage_W, tagerImage_H)) image:image];
        }else{
            // 不大于
            compressionImageData = sourceImageData;
        }
        [imagesData addObject:compressionImageData];
        [images addObject:image];
    }
    self.images = [self.images arrayByAddingObjectsFromArray:images];
    self.imageDatas = [self.imageDatas arrayByAddingObjectsFromArray:imagesData];
}

- (void)hanglePickerImageFromCamera:(UIImage *)photo {
    //图片压缩
    NSData* compressionImageData = nil;
    NSData* sourceImageData = UIImagePNGRepresentation(photo);
    if (sourceImageData.length > stand_size) {
        CGFloat sourceImage_W = CGImageGetWidth(photo.CGImage);
        CGFloat sourceImage_H = CGImageGetHeight(photo.CGImage);
        CGFloat tagerImage_W = 0;
        CGFloat tagerImage_H = 0;
        if (sourceImage_W > CompressionMax_W) {
            tagerImage_W = CompressionMax_W;
            CGFloat scale = sourceImage_W / sourceImage_H;
            tagerImage_H = tagerImage_W / scale;
        }else{
            tagerImage_W = sourceImage_W;
            tagerImage_H = sourceImage_H;
        }
        GACompressionPicHandle* compressPicHandle = [GACompressionPicHandle shareCompressionPicHandle];
        compressionImageData = [compressPicHandle imageByScalingAndCroppingForSize:(CGSizeMake(tagerImage_W, tagerImage_H)) image:photo];
    }else{
        compressionImageData = sourceImageData;
    }
    self.imageDatas = [self.imageDatas arrayByAddingObject:compressionImageData];
    self.images = [self.images arrayByAddingObject:photo];
}

#pragma mark - getter

- (RACCommand *)requestSendCommand {
    if (!_requestSendCommand) {
        _requestSendCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            self.pubParams.content = self.content;
            self.pubParams.imagesData = self.imageDatas;
            return [OSCTweetTools requestTweetPubWithParamsModel:self.pubParams];
        }];
        [_requestSendCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
            // TODO: - HUD
            [self.services dismissViewModelAnimated:YES completion:nil];
        }];
        [_requestSendCommand.errors subscribeNext:^(id x) {
            // TODO: - 错误处理
        }];
    }
    return _requestSendCommand;
}
@end
